﻿//Sensor average report

function SensorAverageReportWin() {
    var pagesize = 60000;
    var day = new Date();
    var day = new Date();
    var today = day.getFullYear() + '-' + padLeft((day.getMonth() + 1).toString(), "0", 2) + '-' + padLeft(day.getDate().toString(), "0", 2);
    var yyday = addDays(day, -1);
    var yestoday = yyday.getFullYear() + '-' + padLeft((yyday.getMonth() + 1).toString(), "0", 2) + '-' + padLeft(yyday.getDate().toString(), "0", 2);

    var trackNameCombo = getTrackerCheckboxCombo(true);

    var fromDateEditor = new Ext.form.DateField({
        renderer: Ext.util.Format.dateRenderer('Y-m-d'),
        format: 'Y-m-d',
        value: today
    });


    var fromTimeEditor = new Ext.form.TimeField({
        value: "00:00",
        format: 'H:i',
        width: 70
    });

    var toDateEditor = new Ext.form.DateField({
        renderer: Ext.util.Format.dateRenderer('Y-m-d'),
        format: 'Y-m-d',
        value: today
    });

    var toTimeEditor = new Ext.form.TimeField({
        value: '23:59',
        format: 'H:i',
        width: 70
    });

    var fields = ["TrackerName", "sssid", "SensorName", "fromtime", "totime","AverageValue"];

    var averagestore = new Ext.data.GroupingStore({
        proxy: new Ext.data.HttpProxy({
            url: "HandlerExtendSensorAverage",
            method: "POST",
            timeout: 120000
        }),
        reader: new Ext.data.JsonReader({
            fields: fields,
            root: "data",
            totalProperty: "totalCount"
        })
        ,sortInfo: { field: 'TrackerName', direction: "ASC" }
        ,groupField: 'TrackerName'
    });
    
    averagestore.addListener('load', function(store, records) {
    
    });

    var cmbQuickTimer = new Ext.form.ComboBox({
        editable: false,
        forceSelection: true,
        disableKeyFilter: true,
        typeAhead: false,
        mode: 'local',
        triggerAction: 'all',
        width: 50,
        store: new Ext.data.ArrayStore({
            fields: [{ name: 'val', type: 'integer' }, 'text'],
            data: [[0, _Time_Today], [1, _Time_Yesterday], [2, _Time_7Days], [3, _Time_ThisMonth], [4, _Time_30Days]]
        }),
        displayField: 'text',
        width: 100,
        valueField: 'val'
    });
    cmbQuickTimer.setValue(0);

    cmbQuickTimer.addListener('select', function(obj, record, index) {
        var val = record.get('val');

        var day = new Date();
        var tmpbegintstr = today;
        var tmpendstr = today;
        if (val == 0) {
            tmpbegintstr = today;
            tmpendstr = today;
        }
        else if (val == 1) {
            tmpbegintstr = yestoday;
            tmpendstr = yestoday;
        }
        else if (val == 2) {
            var tmpd = addDays(day, -6);
            tmpbegintstr = tmpd.getFullYear() + '-' + padLeft((tmpd.getMonth() + 1).toString(), '0', 2) + '-' + padLeft(tmpd.getDate().toString(), '0', 2);
            tmpendstr = today;
        }
        else if (val == 3) {
            var tmpd = addDays(day, -6);
            tmpbegintstr = tmpd.getFullYear() + '-' + padLeft((tmpd.getMonth() + 1).toString(), '0', 2) + '-01';
            tmpendstr = today;
        }
        else if (val == 4) {
            var tmpd = addDays(day, -30);
            tmpbegintstr = tmpd.getFullYear() + '-' + padLeft((tmpd.getMonth() + 1).toString(), '0', 2) + '-' + padLeft(tmpd.getDate().toString(), '0', 2);
            tmpendstr = today;
        }
        fromDateEditor.setValue(tmpbegintstr);
        toDateEditor.setValue(tmpendstr);
        fromTimeEditor.setValue("00:00");
        toTimeEditor.setValue("23:59");
    });

    var tbar = new Ext.Toolbar({
        items: [_Alarm_TrackerName + ':',
        trackNameCombo,
        {
            xtype: 'tbseparator', width: 5
        },
        cmbQuickTimer,
        _Alarm_FromTime + ':',
        fromDateEditor,
        {
            xtype: 'tbseparator', width: 5
        },
        fromTimeEditor,
        {
            xtype: 'tbseparator', width: 5
        },
        _Alarm_ToTime + ':',
        toDateEditor,
        {
            xtype: 'tbseparator', width: 5
        },
        toTimeEditor,
        {
            xtype: 'tbseparator', width: 5
        },{
            tooltip: { text: _TrackerMain_Search, autoHide: true },
            icon: 'images/search16.gif',
            handler: function() {
                averagestore.reload({
                    params: {
                        trackName: trackNameCombo.getRawValue(),
                        fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
                        totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value,
                        start: 0,
                        limit: pagesize
                    }
                });
            }
        },{
            tooltip: { text: _TrackerMain_ExportToExcel, autoHide: true },
            icon: 'images/excel16.gif',
            handler: function() {
                if (!Ext.fly('sensoraverageexcel')) {
                    var frm = document.createElement('form');
                    frm.id = 'sensoraverageexcel';
                    frm.name = id;
                    frm.style.display = 'none';
                    document.body.appendChild(frm);
                }
                Ext.Ajax.request({
                    url: 'HandlerExcel',
                    form: Ext.fly('sensoraverageexcel'),
                    method: 'POST',
                    params: {
                        exceltype: 'sensoraverage',
                        start: 0,
                        limit: 60000,
                        trackName: trackNameCombo.getRawValue(),
                        fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
                        totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value
                    },
                    isUpload: true
                });

            }
        }]
    });

    
    var grdAverage = new Ext.grid.GridPanel({
        store: averagestore,
        loadMask: true,
        columns: [
            { header: _Alarm_Header_Name, width: 300, sortable: true, menuDisabled: true, dataIndex: 'TrackerName' }
            , { header: _Sensor_Name, width: 300, sortable: true, menuDisabled: true, dataIndex: 'SensorName' }
            , { header: _Sensor_Average_Value, width: 150, sortable: true, menuDisabled: true, dataIndex: 'AverageValue' }
        ],
        width: 805,
        height: 420
        ,view: new Ext.grid.GroupingView({
            forceFit: true,
            groupTextTpl: '{gvalue}'
        })
    });

    var sensoraveragewin = new Ext.Window({
        title: _Sensor_Average_Report,
        layout: 'fit',
        height: 490,
        width: 900,
        items: grdAverage,
        tbar: tbar,
        iconCls: 'report-average',
        closeAction: 'close',
        minimizable: true,
        constrain: true,
        listeners: {
            'minimize': function() {
                addTaskButtonForWindow(sensoraveragewin);
            }
        }

    });

    averagestore.on('beforeload', function(store) {
        averagestore.baseParams = {
            start: 'start',
            limit: 'limit',
            trackName: trackNameCombo.getRawValue(),
            fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
            totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value
            
         }
    });

    sensoraveragewin.show();

};